/*********************************************************************************************************************/
/*GAME PROCESS********************************************************************************************************/
/*********************************************************************************************************************/

function Game(){
	
	/***********/
	/*variables*/
	/***********/
	
	var self = this;
	
	//HACK manage this..
	//var ymouse;
	//var xmouse;
	
	//game time to be able to stop the fight with clear(gameTimer);
	var gameTimer;
	
	/***********/
	/*functions*/
	/***********/
	
	this.start = function(){
		gameTimer = setInterval(self.gameUpdate,60);
	}
	
	this.gameUpdate = function(){
		/*main loop for the game*/
		//need to be other things than displaying, as all game process :/
		
		// --- display part ---
		displayer.updateWorld();
	}

	function mouseCoodToHexCood(x,y) {
		/*function that take mouse cood in pixel and return the coods of the tile selected*/
		
		//cood to return
		var xHex;
		var yHex;
		
		//constant for squares
		var cos_main_grid_x = 0.882947593; //11.2Pi/72
		var sin_main_grid_x = 0.469471563;
		var cos_main_grid_y = -0.130526192; //39Pi/72
		var sin_main_grid_y = 0.991444861;
		
		var cos_other_grid_x = 0.4539905; //25.2Pi/72
		var sin_other_grid_x = 0.891006524;
		var cos_other_grid_y = -0.882947593; //60.8Pi/72
		var sin_other_grid_y = 0.469471563;
		
		//column of the mouse clic (main)
		//Note that : 55/173.2 = f(tileUnit,sin,cos) but I don't know what f is :/ -> So motor doesnt support tileUnit changes. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		var nb = Math.floor(((y-(cos_main_grid_x/sin_main_grid_x)*x-(mapY+0)+(cos_main_grid_x/sin_main_grid_x)*(mapX+320))/(55*cos_main_grid_x)));
		var nb2 = Math.floor(((y-(cos_main_grid_y/sin_main_grid_y)*x-(mapY+480)+(cos_main_grid_y/sin_main_grid_y)*(mapX+206))/(173.2*cos_main_grid_y)));
		
		//constantes for maths depending on which square on the hex you are
		var xConstante = 9 + (self.mod(nb,3));
		var yConstante = 20 + (self.mod(nb2,2));
		if (nb2%2 == 0) {yConstante += self.mod(nb+1,2);}
		
		//get tile where mouse is, without considering a bug and multiple tiles squares
		xHex = Math.floor(((xConstante-nb)/6)+((yConstante-nb2)/2));
		yHex = Math.floor(((nb-xConstante)/4)+((yConstante-nb2)/4));
		
		//adjust a calcul bug that happen on a regular basis
		if (self.mod(nb,6)==1 && nb2%2==0){
			xHex++;
			if (nb2%4==0 && self.mod(nb,12)!=7){yHex++;}
			if (self.mod(nb2,4)==2 && self.mod(nb,12)==7){yHex++;}
		}
		
		//adjust in case of multiple tiles square (square where there is 2 tiles)
		if (nb%3==0) {		
			if ((nb2%2==0 && nb%6==0) || (self.mod(nb2,2)==1 && self.mod(nb,6)==3)) {
				//column number where the mouse clicked in secondary grid
				var nb3 = Math.floor(((y-(cos_other_grid_x/sin_other_grid_x)*x-(mapY+0)+(cos_other_grid_x/sin_other_grid_x)*(mapX+301))/(68*cos_other_grid_x)));
				//column number where it shouldn't be
				var nbCheck = 3+2*yHex+(xHex%2);
				//if wrong column number then change to the right tile
				if (nb3 == nbCheck) {yHex--;}
			}
			else {
				//same as if but in cas of other multiple tiles square
				var nb4 = Math.floor(((y-(cos_other_grid_y/sin_other_grid_y)*x-(mapY+480)+(cos_other_grid_y/sin_other_grid_y)*(mapX+212.5))/(95.75*cos_other_grid_y)));
				var nbCheck = 7-Math.floor(3*xHex/2)+yHex;
				if (nb4 == nbCheck) {
					if (xHex%2 == 0) {yHex--;}
					xHex++;
				}
			}
		}
		
		return [xHex,yHex];
		
	}


	this.mod = function(n, mod) {
		/*function that return n%mod - hack for negative numbers --- Ex : mod(-5,6) return 1 instead of -5. */
		//It counter a bug when algo used %6 to mean "do it 1 tile out of 6" and coods were negative
		return ((mod+(n%mod))%mod);
	}

	this.mouseCheck = function(e){
		/*function that happens when you move your move, calculate on which tile your mouse is*/
		
		//mouse coods					
		var x;
		var y;
		if (e.pageX || e.pageY) { 
			x = e.pageX;
			y = e.pageY;
		}
		else { 
			x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 
			y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; 
		} 
		
		//mouse coods in canvas
		x -= canvas.offsetLeft;
		y -= canvas.offsetTop;
		
		//get on which tile is mouse
		[xmouse,ymouse] = mouseCoodToHexCood(x,y);
		
	}
}